#!/usr/bin/perl
#
use strict;
use warnings;
use Data::Dumper;
 
binmode STDOUT, ":utf8";
#use utf8;
 
use JSON;

require "doa_subs.pl";
use CGI;
use lib '.';
use users;

my ($username,$level) = &validate;
if ($level > 5) { &dienice("You must be an administrator to run this");}
my ($realm) = get_realm();
my $realmdesc=get_realm_desc($realm);
############################## Main Prog ##############################
my $q = new CGI;

print $q->header;
print $q->start_html(
	-title => "Process Alliance Update file"
);

print $q->h1("Process Alliance update file for Realm ($realmdesc)");

my $player_id={};
my $player_name={};
my $hashcnt1=0;
my $hashcnt2=0;

my $outdata={};

$outdata=loadMAP("mapfile");

my $player_count=loadPlayers($player_id,$player_name);
$hashcnt1=keys %{$player_id};
$hashcnt2=keys %{$player_name};
#print $q->p("initial player count $player_count playerid  $hashcnt1 player name $hashcnt2\n");

my $alliance_id={};
my $alliance_name={};
my $processed_files=0;

my $alliance_count=loadAlliance($alliance_id,$alliance_name);
my @allFiles=readDir("alliance");
foreach my $aname (@allFiles)
{
	my $trealm=fileREALM($aname);
	#warn "$trealm $realm";
	next if $trealm ne $realm;
	#my $aname=@allFiles[scalar(@allFiles-1)];
	my $aullname="alliance/".$aname;	#
	print $q->p("Processing Alliance update file $aname\n");
	$processed_files++;
	my $allidata = readJSON($aullname);
	my $alliref = $allidata->{'alliances'};
	my $foeref=$allidata->{'foes'};
	my $friendref=$allidata->{'friends'};
	my $aid="";
	my $name="";
	my $state="";
	my $rank="";
	my $lord="";
	my $lord_id="";
	my $members="";
	my $matched="";
	
	my $friends={};
	my $foes={};
	
	foreach my $friend (@$friendref)
	{
	#	print "friend $friend\n";
		$friends->{$friend}="y";
	}
	
	my $friend_count=0;
	$friend_count=keys %{$friends};
	$outdata->{'update'}{'friends'}=$friend_count;
	
	foreach my $foe (@$foeref)
	{
	#	print "foe $foe\n";
		$foes->{$foe}="y";
	}
	my $foe_count=0;
	$foe_count=keys %{$foes};
	$outdata->{'update'}{'foes'}=$foe_count;
	
	foreach my $allikey (@$alliref)
	{
		$aid=$allikey->{'id'};
	    $name=$allikey->{'name'};
		$rank=$allikey->{'rank'};
		$lord=$allikey->{'lord'};
		$lord_id=$allikey->{'lord_id'};
		$members=$allikey->{'count'};
		$state="";
		
		if (defined $friends->{$aid})
		{
			$state="friend";
		}
		if (defined $foes->{$aid})
		{
			$state="foe";
		}
		
	    $alliance_id->{$aid}={'name'=>$name, 'state'=>$state,'rank'=>$rank,'lord'=>$lord,'members'=>$members};
		$player_id->{$lord_id}=$lord;
	}
	
	
	$alliance_count=writeAlliance($alliance_id);
	$player_count=writePlayers($player_id);
	
	$hashcnt1=keys %{$player_id};
	$hashcnt2=keys %{$player_name};
#	print $q->p("after final write player count $player_count playerid  $hashcnt1 player name $hashcnt2\n");
#	print $q->p("after final write Alliance count $alliance_count\n");
	$outdata->{'update'}{'alliance'}=time;
	writeMAP($outdata,"mapfile");
	move("$aullname","processed_alliance/$aname") or die_nice ("Could not move $aname to processed_alliance directory");
}	

print $q->br();
print $q->p("Processed $processed_files Files");
print $q->br();
print $q->a( {-href=>"menu.cgi"}, "Main Menu");
print $q->end_html;


